package com.hxg.bl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxg.bl.bean.AppointmentItemBean;
import com.hxg.bl.dao.jpa.AppointmentItemBeanJpa;
import com.hxg.bl.dao.mapper.AppointmentItemMapper;
import com.hxg.bl.service.AppointmentItemService;
import com.hxg.bl.vo.SearchVo;
import com.hxg.common.LayuiPageInfo;
import com.hxg.common.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Transactional
@Service("appointmentItemService")
public class AppointmentItemServiceImpl implements AppointmentItemService {

    @Autowired
    private AppointmentItemBeanJpa appointmentItemBeanJpa;

    @Autowired
    private AppointmentItemMapper appointmentItemMapper;

    @Autowired
    private CommonService commonService;

    @Override
    public void saveAndFlushAppointmentItem(AppointmentItemBean appointmentItemBean) {
        if (appointmentItemBean.getId() == null) {
            appointmentItemBean.setOrderNum(appointmentItemBean.getOrderNum() - 1);
            appointmentItemBeanJpa.saveAndFlush(appointmentItemBean);
        }

        String tableName = "cm_appointment_item";
        commonService.dealOrderNum(appointmentItemBean, appointmentItemBeanJpa.getOne(appointmentItemBean.getId()), tableName);
        appointmentItemBeanJpa.saveAndFlush(appointmentItemBean);
        commonService.reSortOrderNum(tableName);
    }

    @Override
    public PageInfo<AppointmentItemBean> listAppointmentItemByParams(Integer pn, Integer pageSize,
                                                                     SearchVo searchVo) {
        PageHelper.startPage(pn, pageSize);

        List<AppointmentItemBean> navTabBeanList = appointmentItemMapper.listAppointmentItemByParams(searchVo);

        return new LayuiPageInfo<>(navTabBeanList);
    }

    @Override
    public void batchDelAppointmentItem(List<Long> idList) {
        appointmentItemBeanJpa.deleteAllByIdIn(idList);
        String tableName = "cm_appointment_item";
        commonService.reSortOrderNum(tableName);
    }
}
